import { baseInputVars, LLMNode } from "../../../core";
import { Annotation, AnnotationRoot } from '@langchain/langgraph';
import { ImageExtNode } from "../../genPosterWork/nodes/imageExtNode";
import { IEditItem } from "../data";


export class ImageListExtNode extends LLMNode {
    name = "ImageListExtNode";
    inputVars = Annotation.Root({
        ...baseInputVars.spec,
        editList: Annotation<IEditItem[]>
    });

    outputVars = Annotation.Root({
    });

    private async imageExItem(editItem: IEditItem) {
        if (editItem.imageList.length == 0) {
            return {
                objID: editItem.objID,
                imageExt: []
            }
        }

        let node = new ImageExtNode();
        node.inputVars.State = {
            imageList: editItem.imageList,
            ...this.inputVars.State
        };
        node.outputVars.State = {
            imageExt: "",
            imageList: []
        }

        await node.Run();

        return {
            objID: editItem.objID,
            imageExt: JSON.parse(node.outputVars.State.imageExt)
        }
    }

    async Run() {
        const plist = [];
        for (let item of this.inputVars.State.editList) {
            plist.push(this.imageExItem(item));
        }

        const result = await Promise.all(plist);

        this.outputVars.State[this.ResultField] = result;

        return this.outputVars.State;
    }
}